/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Reinhard
 */
public class AchievementModel {
  Connection connection;
  public final int POST_ONCE = 1;
  public final int POST_10 = 2;
  public final int COMMENT_ONCE = 3;
  public final int COMMENT_20 = 4;
  public final int ONE_POST_100_LIKES = 5;
  public final int ONE_POST_100_DISLIKES = 6;
  public final int EASTER_EGG = 7;

  public AchievementModel(Connection connection) {
    this.connection = connection;
  }

  //mendapatkan achievement apa aja yang sudah didapat oleh user
  public ResultSet getAllAchievementAchieved(String user_id) throws SQLException {
    String query = "SELECT * FROM achievement LEFT JOIN achieving ON achievement.ID = achieving.achievement_id"
            + " WHERE user_id = '"
            + user_id
            + "'";
//    System.out.println(query);
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = statement.executeQuery(query);
    return rs;
  }

  //mendapatkan daftar achievement yang bisa diraih
  public ResultSet getAllAchievement() throws SQLException {
    String query = "SELECT * FROM achievement";
//    System.out.println(query);
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = statement.executeQuery(query);
    return rs;
  }

  public ResultSet getAchievement(String achievement_id) throws SQLException {
    String query = "SELECT * FROM achievement WHERE ID = '"
            + achievement_id
            + "'";
//    System.out.println(query);
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = statement.executeQuery(query);
    return rs;
  }

  public void addAchievementToUser(String user_id, int achievement_id) throws SQLException {
    String query = "INSERT INTO achieving(user_id, achievement_id) VALUES('"
            + user_id
            + "','"
            + achievement_id
            + "')";
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    
    try {
        statement.executeUpdate(query);
    }catch(SQLException ex) {
        //ignore        
    }    
  }
}
